package com.chen;

//import java.io.IOException;
//import java.net.Socket;
//import java.net.UnknownHostException;
//
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
//import android.hardware.Sensor;
//import android.hardware.SensorEvent;
//import android.hardware.SensorEventListener;
//import android.hardware.SensorManager;
//import android.hardware.Sensor;
//import android.hardware.SensorEvent;
//import android.hardware.SensorEventListener;
//import android.hardware.SensorListener;
//import android.hardware.SensorManager;
import android.os.Bundle;

public class mainui extends Activity {
    /** Called when the activity is first created. */
	private SensorManager _sensorMnr;
	private Socket conn ;
	private ContrlMsg data;
	private PrintWriter pw;
	private BufferedReader br;
	
	private SensorListener _sensorSnl ;
	private class HandleSensorChange  implements SensorListener {

		public void onAccuracyChanged(int sensor, int accuracy) {
			// TODO Auto-generated method stub
			
		}

		public void onSensorChanged(int sensor, float[] values) {
			// TODO Auto-generated method stub
			if (sensor == SensorManager.SENSOR_ACCELEROMETER){
				data.setXYZ(values[0], values[1], values[2]);
				if (conn.isBound()){
					sendData();
				}
			}
		}
 
	}
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
			conn = new Socket("localhost",7090);
			conn.setKeepAlive(true);
			conn.connect(conn.getLocalSocketAddress());
			br = new BufferedReader(new InputStreamReader( conn.getInputStream()));
			pw = new PrintWriter(conn.getOutputStream());
			sendData();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.data = new ContrlMsg();
		this._sensorMnr = (SensorManager)getSystemService(SENSOR_SERVICE);
		this._sensorSnl = new HandleSensorChange();
		this._sensorMnr.registerListener(_sensorSnl, SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_GAME);
		System.out.println("x:"+data.getX() + ",y:"+ data.getY()+",z"+data.getZ());
		
    }
	
	public void sendData(){
		if (!this.conn.isConnected())
			return;
		String data = dataMarshell.Object2String(this.data);
		pw.write(data);
		pw.flush();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (this.conn != null){
			try {
				this.pw.close();
				this.br.close();
				this.conn.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}